package org.example.mock;

import lombok.extern.slf4j.Slf4j;
import org.example.bean.MovieScoreVotecount;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 生成 MovieScoreVotecount 模拟数据并插入数据库
 */
@Slf4j
public class MovieScoreVotecountGenerator {
    private static final String DB_URL = "jdbc:mysql://192.168.10.102/changchengfilm?requireSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "000000";
    private static final int BATCH_SIZE = 100;
    // 存储生成的数据，按需使用
    private static final List<MovieScoreVotecount> DATA_LIST = new ArrayList<>();
    private static final Random RANDOM = new Random();
    // 预设的评分区间示例，可根据实际需求调整、扩充
    private static final String[] SCORE_GROUPS = {"0.0-3.0", "3.1-6.0", "6.1-8.0", "8.1-10.0"};

    public static void main(String[] args) {
        log.info("开始生成 MovieScoreVotecount 模拟数据");
        List<MovieScoreVotecount> result = generateData(SCORE_GROUPS.length);
        log.info("生成完成，开始插入数据库");

        try {
            insertDataIntoDatabase(result);
            log.info("数据插入成功");
        } catch (SQLException e) {
            log.error("数据库插入失败", e);
        }
    }

    /**
     * 生成指定数量的模拟数据
     * @param count 数据条数，这里根据预设评分区间数量决定
     * @return 包含模拟数据的列表
     */
    private static List<MovieScoreVotecount> generateData(int count) {
        List<MovieScoreVotecount> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            String scoreGroup = SCORE_GROUPS[i];
            // 解析评分区间，生成 minScore 和 maxScore，这里简单示例，可更严谨处理
            String[] parts = scoreGroup.split("-");
            BigDecimal minScore = new BigDecimal(parts[0]);
            BigDecimal maxScore = new BigDecimal(parts[1]);
            // 随机生成平均投票数，范围可调整
            int avgVoteCount = RANDOM.nextInt(2000) + 500;
            // 随机生成电影数量，范围可调整
            int movieCount = RANDOM.nextInt(100) + 20;

            MovieScoreVotecount data = new MovieScoreVotecount(scoreGroup, minScore, maxScore, avgVoteCount, movieCount);
            list.add(data);
            DATA_LIST.add(data);
        }
        return list;
    }

    /**
     * 将生成的数据插入数据库
     * @param dataList 待插入的数据列表
     * @throws SQLException 数据库操作异常
     */
    private static void insertDataIntoDatabase(List<MovieScoreVotecount> dataList) throws SQLException {
        String sql = "INSERT INTO movie_score_votecount (score_group, min_score, max_score, avg_vote_count, movie_count) " +
                "VALUES (?, ?, ?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            conn.setAutoCommit(false); // 开启事务

            for (MovieScoreVotecount data : dataList) {
                pstmt.setString(1, data.getScoreGroup());
                pstmt.setBigDecimal(2, data.getMinScore());
                pstmt.setBigDecimal(3, data.getMaxScore());
                pstmt.setInt(4, data.getAvgVoteCount());
                pstmt.setInt(5, data.getMovieCount());
                pstmt.addBatch();
            }

            int[] results = pstmt.executeBatch();
            conn.commit(); // 提交事务

            log.info("成功插入 {} 条记录", results.length);
        } catch (SQLException e) {
            log.error("执行数据库插入时发生异常", e);
            throw e;
        }
    }
}